package org.jsbd.boss.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IMiguMusicConfigDao;
import org.jsbd.boss.domian.MiguMusicConfigBean;
import org.jsbd.boss.service.IMiguMusicConfigService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.TimeModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("miguMusicConfigService")
public class MiguMusicConfigServiceImpl implements IMiguMusicConfigService {

	@Autowired
	private IMiguMusicConfigDao miguMusicConfigDao;
	@Autowired
	private RedisClient<String,Object> redisClientMigu;

	@Override
	public void saveMiguMusicConfig(MiguMusicConfigBean miguMusicConfig){
		if(miguMusicConfig.getTotalNum()==null){
			miguMusicConfig.setTotalNum(0L);
		}
		
		if(StringUtils.isBlank(miguMusicConfig.getVersion())){
			miguMusicConfig.setVersion("6.2.2");
		}
		
		if(StringUtils.isBlank(miguMusicConfig.getSignVersion())){
			miguMusicConfig.setSignVersion("V001");
		}
		miguMusicConfig.setDeleteFlag(0);
		miguMusicConfig.setStatus(0);
		miguMusicConfigDao.saveMiguMusicConfig(miguMusicConfig);
	}

	@Override
	public void updateMiguMusicConfig(MiguMusicConfigBean miguMusicConfig){
		if(miguMusicConfig.getTotalNum()==null){
			miguMusicConfig.setTotalNum(0L);
		}
		if(StringUtils.isBlank(miguMusicConfig.getVersion())){
			miguMusicConfig.setVersion("6.2.2");
		}
		
		if(StringUtils.isBlank(miguMusicConfig.getSignVersion())){
			miguMusicConfig.setSignVersion("V001");
		}
		miguMusicConfigDao.updateMiguMusicConfig(miguMusicConfig);
	}

	@Override
	public MiguMusicConfigBean getMiguMusicConfig(MiguMusicConfigBean miguMusicConfig) {
		return miguMusicConfigDao.getMiguMusicConfig(miguMusicConfig);
	}

	@Override
	public List<MiguMusicConfigBean> getMiguMusicConfigList(MiguMusicConfigBean miguMusicConfig) {
		return miguMusicConfigDao.getMiguMusicConfigList(miguMusicConfig);
	}

	@Override
	public Map<String, Object> getMiguMusicConfigListPage(DataGridModel page, MiguMusicConfigBean miguMusicConfig) {
		Map<String,Object> results=miguMusicConfigDao.getMiguMusicConfigListPage(page, miguMusicConfig);
		
		List<MiguMusicConfigBean> configList=(List<MiguMusicConfigBean>)results.get("rows");
		if(configList==null||configList.isEmpty()){
			return results;
		}
		
		
		Map<Object,Object> successMap=redisClientMigu.getHash(CacheKey.MIGUMUSIC_SUCCESS_NUM_KEY);
		Map<Object,Object> doNumMap=redisClientMigu.getHash(CacheKey.MIGUMUSIC_NEW_DO_NUM_HASH_KEY);
		
		String sucessPrefixKey=CacheKey.MIGUMUSIC_SUCCESS_NUM_KEY;
		String doPrefixKey=CacheKey.MIGUMUSIC_NEW_TASK_NUM_DONE;
		
		String doDay=DateUtil.getCurrentDateStr();

		for(MiguMusicConfigBean config:configList){
			String suffixKey=doPrefixKey+config.getChannelId()+"_"+config.getVersion()+"_"+doDay;
			Long userNum=(Long) doNumMap.get(suffixKey+"_user");
			if(userNum==null){
				userNum=0L;
			}
			Long noUserNum=(Long)doNumMap.get(suffixKey+"_nouser");
			if(noUserNum==null){
				noUserNum=0L;
			}
			long doNum=userNum+noUserNum;
			String successKey=sucessPrefixKey+","+config.getChannelId()+","+config.getVersion()+","+doDay;
			String successNum=(String)successMap.get(successKey);
			if(StringUtils.isBlank(successNum)){
				successNum="0";
			}
			
			Long totalNum=config.getTotalNum();
			if(totalNum==null){
				totalNum=0L;
			}
			int planNum=TimeModelUtils.genMaxNumByTime(Integer.valueOf(totalNum+""), new Date());
			
			String totalNumStr=planNum+"|"+doNum+"|"+successNum;
			config.setTotalNumStr(totalNumStr);
		}
		
		return results;
	}

	@Override
	public Integer getMiguMusicConfigCount(MiguMusicConfigBean miguMusicConfig) {
		return miguMusicConfigDao.getMiguMusicConfigCount(miguMusicConfig);
	}
	
	@Override
	public void deleteMiguMusicConfig(MiguMusicConfigBean miguMusicConfig) {
		miguMusicConfigDao.deleteMiguMusicConfig(miguMusicConfig);
	}

	@Override
	public void updateMiguMusicStatus(MiguMusicConfigBean miguMusicConfig) {
		miguMusicConfigDao.updateMiguMusicStatus(miguMusicConfig);
	}
	
}
